let username=document.querySelector('.username')
let password=document.querySelector('.password')
let username_reg=/^\w{3,11}$/
let password_reg=/^\w{6,18}$/
let complete=[0,0]
username.oninput=()=>{
    if(username_reg.test(username.value)){
        tip(username.nextElementSibling.nextElementSibling,1)
        complete[0]=1
}else{
        tip(username.nextElementSibling.nextElementSibling,2)
        complete[0]=0;
    }
}
password.oninput=()=>{
    if(password_reg.test(password.value)){
        tip(password.nextElementSibling.nextElementSibling,1)
        complete[1]=1
    }else{
        tip(password.nextElementSibling.nextElementSibling,2)
        complete[1]=0
    }
}
// 判断正确
function tip(changeElement,i){
    if (i==1) {
        changeElement.style.display='none';
    }else if(i==2){
        changeElement.style.display='block';
        changeElement.style.color='#f00';
        changeElement.style.fontWeight='700';

    }
}

let loginbtn=document.querySelector('button')
loginbtn.onclick=(evt)=>{
    evt.stopPropagation();
    if(complete.join('')==11){
            let loginURL=`http://jx.xuzhixiang.top/ap/api/login.php`
            axios.get(loginURL,{params:{
                username:username.value,
                password:password.value,
            }}).then(function(resolve){
                console.log(resolve.data);
                if(resolve.data.code==1){
                    let user=resolve.data.data;
                    localStorage.setItem('uid',user.id)
                    localStorage.setItem('username',user.username)
                    localStorage.setItem('password',user.password)
                    localStorage.setItem('token',user.token)
                    document.querySelector('.wrap').style.display='block'
                    document.querySelector('.wrap div p:first-child span').innerHTML=`${username.value}`            
                    let times=4
                    setInterval(() => {
                        if (times==0) {
                            location.href='../index.html?'
                        }
                        document.querySelector('.wrap div p:last-child').innerHTML=`精彩马上开始...(${times})`
                        times--;
                    }, 1000);        
                }else{
                    let tips=document.querySelector('.wrap')
                    tips.style.display='block';
                    document.querySelector('.wrap div p:first-child').innerHTML=`请检查账号、密码是否输入正确`
                    document.querySelector('.wrap div p:last-child').innerHTML=`点击任意处可关闭...`
                    document.onclick=()=>{
                        tips.style.display='none';
                    }            
                }
            }).catch(function(reject){
                console.log(reject);
            });
    }else{
        let tips=document.querySelector('.wrap')
        tips.style.display='block';
        document.querySelector('.wrap div p:first-child').innerHTML=`账号密码都不填还想登录？`
        document.querySelector('.wrap div p:last-child').innerHTML=`点击任意处可关闭...`
        document.onclick=()=>{
            tips.style.display='none';
        }
    }

}